﻿
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ufaev.Cryptography.Test
{
	/// <summary>
	/// Summary description for Gost3411Tests.
	/// </summary>
	[TestClass]
	public class Gost3411Tests
	{
		#region Fields

		private TestContext testContextInstance;

		#endregion Fields

		#region Properties

		/// <summary>
		/// Gets or sets the test context which provides
		/// information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get { return testContextInstance; }
			set { testContextInstance = value; }
		}

		#endregion Properties

		#region Constructors

		public Gost3411Tests()
		{
		}

		#endregion Constructors

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		#region Methods

		[TestMethod]
		public void Example1()
		{
			string text = "This is message, length=32 bytes";
			byte[] inputData = Encoding.GetEncoding(1251).GetBytes(text);

			// Expected result.
			byte[] expectedResult = new byte[32] {
				0xb1, 0xc4, 0x66, 0xd3, 0x75, 0x19, 0xb8, 0x2e,
				0x83, 0x19, 0x81, 0x9f, 0xf3, 0x25, 0x95, 0xe0,
				0x47, 0xa2, 0x8c, 0xb6, 0xf8, 0x3e, 0xff, 0x1c,
				0x69, 0x16, 0xa8, 0x15, 0xa6, 0x37, 0xff, 0xfa
			};

			RunTest(inputData, expectedResult);
		}

		[TestMethod]
		public void Example2()
		{
			string text = "Suppose the original message has length = 50 bytes";
			byte[] inputData = Encoding.GetEncoding(1251).GetBytes(text);

			// Expected result.
			byte[] expectedResult = new byte[32] {
				0x47, 0x1a, 0xba, 0x57, 0xa6, 0x0a, 0x77, 0x0d,
				0x3a, 0x76, 0x13, 0x06, 0x35, 0xc1, 0xfb, 0xea,
				0x4e, 0xf1, 0x4d, 0xe5, 0x1f, 0x78, 0xb4, 0xae,
				0x57, 0xdd, 0x89, 0x3b, 0x62, 0xf5, 0x52, 0x08
			};

			RunTest(inputData, expectedResult);
		}

		[TestMethod]
		public void Example3()
		{
			byte[] inputData = new byte[0];

			// Expected result.
			byte[] expectedResult = new byte[32] {
				0xce, 0x85, 0xb9, 0x9c, 0xc4, 0x67, 0x52, 0xff,
				0xfe, 0xe3, 0x5c, 0xab, 0x9a, 0x7b, 0x02, 0x78,
				0xab, 0xb4, 0xc2, 0xd2, 0x05, 0x5c, 0xff, 0x68,
				0x5a, 0xf4, 0x91, 0x2c, 0x49, 0x49, 0x0f, 0x8d
			};

			RunTest(inputData, expectedResult);
		}

		[TestMethod]
		public void Example4()
		{
			string text = "The quick brown fox jumps over the lazy dog";
			byte[] inputData = Encoding.GetEncoding(1251).GetBytes(text);

			// Expected result.
			byte[] expectedResult = new byte[32] {
				0x77, 0xb7, 0xfa, 0x41, 0x0c, 0x9a, 0xc5, 0x8a,
				0x25, 0xf4, 0x9b, 0xca, 0x7d, 0x04, 0x68, 0xc9,
				0x29, 0x65, 0x29, 0x31, 0x5e, 0xac, 0xa7, 0x6b,
				0xd1, 0xa1, 0x0f, 0x37, 0x6d, 0x1f, 0x42, 0x94
			};

			RunTest(inputData, expectedResult);
		}

		[TestMethod]
		public void Example5()
		{
			string text = "The quick brown fox jumps over the lazy cog";
			byte[] inputData = Encoding.GetEncoding(1251).GetBytes(text);

			// Expected result.
			byte[] expectedResult = new byte[32] {
				0xa3, 0xeb, 0xc4, 0xda, 0xaa, 0xb7, 0x8b, 0x0b,
				0xe1, 0x31, 0xda, 0xb5, 0x73, 0x7a, 0x7f, 0x67,
				0xe6, 0x02, 0x67, 0x0d, 0x54, 0x35, 0x21, 0x31,
				0x91, 0x50, 0xd2, 0xe1, 0x4e, 0xee, 0xc4, 0x45
			};
			
			RunTest(inputData, expectedResult);
		}

		private void RunTest(byte[] inputData, byte[] expectedResult)
		{
			Gost3411Managed hash = new Gost3411Managed();
			byte[] experimentalHash = hash.ComputeHash(inputData);

			CollectionAssert.AreEqual(expectedResult, experimentalHash);
		}

		#endregion Methods
	}
}
